//----------------------------------------------------------
    //一些与实现日志系统相关的功能类的设计
        //1. 获取当前系统时间
        //2. 判断文件是否存在
        //3. 获取文件的所在目录路径
        //4. 创建目录
//----------------------------------------------------------
#ifndef __LOG_UTIL__
#define __LOG_UTIL__

#include <string>
#include <sys/stat.h>
#include <ctime>
#include <iostream>

#if __cplusplus>=201703L
#include <filesystem>
namespace fs=std::filesystem;
#endif

#if __cplusplus>=201103L
#include <chrono>
using namespace std::chrono;
#endif

namespace ymbxlog{
    namespace Util{
        class Date{
        public:
            static time_t Now()
            {
#if __cplusplus >= 201103L
                // std::cout << "this is c++11" << std::endl;
                return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
#else
                return time(nullptr);
#endif
            }
        };
        class File{
        public:
            static bool IsExist(const std::string& pathname){
#if __cplusplus>=201703L
                // std::cout << "this is c++17" << std::endl;
                return fs::exists(fs::path(pathname));
#else
                struct stat st;
                return (stat(pathname.c_str(), &st) != -1);
#endif
            }
            static std::string GetDir(const std::string &pathname)
            {
#if __cplusplus>=201703L
                fs::path p(pathname);
                return p.parent_path().string();
#else
                if(pathname.empty())
                    return ".";
                size_t pos = pathname.find_last_of("/\\");
                if(pos==std::string::npos)
                    return ".";
                return pathname.substr(0, pos+1);
#endif
            }
            static void CreatDir(const std::string& pathname){
                if(pathname.empty()) return;
                if(IsExist(pathname)) return;
#if __cplusplus>=201703L
                // std::cout<<"this is c++17 2 "<<std::endl;
                fs::path p(pathname);
                create_directories(p);
                return;
#else
                size_t pos = 0,idx=0;
                while(pos<pathname.size()){
                    pos = pathname.find_first_of("/\\", idx);
                    if(pos==std::string::npos){
                        mkdir(pathname.c_str(),0755);
                        return;
                    }
                    if(pos==idx){
                        idx = pos + 1;
                        continue;
                    }
                    std::string str = pathname.substr(0, pos);
                    if (str=="." || str==".."){
                        idx = pos + 1;
                        continue;
                    }
                    if(IsExist(str)){
                        idx = pos + 1;
                        continue;
                    }
                    mkdir(str.c_str(), 0755);
                    idx = pos + 1;
                }
                return;
#endif
            }
        };
    }
}


#endif